# -*- coding: utf-8 -*-
# @Author : gongyu
# @Date   : 2019/4/8
# @File   : IC10_CurrentMaxOverdueMon.py
# @E-mail : gongyu@bbdservice.com

'''eof
name:最大逾期或透支月数
code:IC10_CurrentMaxOverdueMon
tableName:
columnName:
groups:企业征信
dependencies:EC_DSJ
type:常用指标
dataSourceType:
eof'''


import sys
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None:
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if len(table) == 0:
            return [{}]
        elif table is None or isinstance(table, list) is False:
            return None
        else:
            return data.get(tableName)
def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False

def IC10_CurrentMaxOverdueMon():
    data = IC_DSJ['data']
    null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']
    try:
        #  逾期（透支）汇总信息段(PC02D)-逾期（透支）汇总信息(PC02DH)·
        table1 = getTable(data, "PCO", 1)
        if table1 is None:
            return u"缺失值"
        elif table1 == [{}]:
            return u"缺失值"
        else:
            table2 = getTable(table1[0], "PC02",1)
            if table2 is None:
                return u"缺失值"
            elif table2 == [{}]:
                return u"缺失值"
            else:
                table3 = getTable(table2[0], "PC02D", 0)
                if table3 is None:
                    return u"缺失值"
                elif table3 == [{}]:
                    return u"缺失值"
                else:
                    table4 = getTable(table3[0], "PC02DH", 0)
                    if table4 is None:
                        return u"缺失值"
                    elif table4 == [{}]:
                        return u"缺失值"
                    else:
                        factor1=[]
                        factor2=None
                        factor3=None
                        for i in table4:
                            #账户类型
                            PC02DD01=i.get('PC02DD01')
                            #月份数
                            PC02DS03 = i.get('PC02DS03')
                            if PC02DD01 not in null_type_list and isfloat(PC02DS03) is True:
                                if PC02DD01 in ("1","2","3") and float(PC02DS03)>0:
                                    factor1.append(float(PC02DS03))
                                elif PC02DD01 =="4" and float(PC02DS03)>0:
                                    factor2=float(PC02DS03)
                                elif PC02DD01 =="5" and float(PC02DS03)>0:
                                    factor3=float(PC02DS03)
                    factor=sum(factor1)
                    return max(factor,factor2,factor3)
    except:
        return u"缺失值"

result=IC10_CurrentMaxOverdueMon()